'use strict';
var urlArray = window.location.href.split("/");
var urlAction = urlArray[3];

var ajax = {
	initMeun : function() {
		$
				.ajax({
					dataType : "json",
					url : "/menus",
					type : "get",
					async : false,
					contentType : 'application/json;charset=utf-8',
					success : function(rs) {
						if (rs.success) {
							var menu = "<li><a href='../index'> <i class='fa fa-dashboard'></i> <span>首页</span></a></li>";
							// TODO
							for (var i = 0; i < rs.result.length; i++) {
								var activeFlag = false;

								menu = menu
										+ "<li class='treeview activeFlag'>"
										+ "<a href='#'>"
										+ "<i class='fa fa-bar-chart-o'></i>"
										+ "<span>" + rs.result[i].resource_desc
										+ "</span> "
										+ "<i class='arrowStyle'></i>" + "</a>"
										+ "<ul class='treeview-menu'>";
								for (var j = 0; j < rs.result[i].childs.length; j++) {
									var action = rs.result[i].childs[j].resource_String
											.replace("/", "");
									if (urlAction == action) {
										activeFlag = true;
										menu = menu
												+ "<li class='active'><a th:href='@{"
												+ rs.result[i].childs[j].resource_String
												+ "}' href='"
												+ rs.result[i].childs[j].resource_String
												+ "'><i class='fa fa-angle-double-right'></i>"
												+ rs.result[i].childs[j].resource_desc
												+ "</a></li>";
									} else {
										menu = menu
												+ "<li><a th:href='@{"
												+ rs.result[i].childs[j].resource_String
												+ "}' href='"
												+ rs.result[i].childs[j].resource_String
												+ "'><i class='fa fa-angle-double-right'></i>"
												+ rs.result[i].childs[j].resource_desc
												+ "</a></li>";
									}

								}
								menu = menu + "</ul></li>";

								if (activeFlag) {
									menu = menu.replace("activeFlag", "active");
									menu = menu.replace("arrowStyle",
											"fa pull-right fa-angle-down");
								} else {
									menu = menu.replace("activeFlag", "");
									menu = menu.replace("arrowStyle",
											"fa fa-angle-left pull-right");
								}

							}
							$(".sidebar-menu").append(menu);
						} else {
							$("#myModal").text("Hello world!");
						}
					}
				});
	},
	// 包括新增和删除
	addOrUpdate : function(url, data, successTips, errorTips) {
		$.ajax({
			url : url,
			type : "POST",
			async : false,
			dataType : "json",
			data : data,
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					App.myDialog(successTips, "提示");
				} else {
					App.myDialog(errorTips + rs.result, "提示");
				}
			}
		});
	},
	delete : function(url, successTips, errorTips) {
		$.ajax({
			url : url,
			type : "GET",
			async : false,
			dataType : "json",
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					App.myDialog(successTips, "提示");
				} else {
					App.myDialog(errorTips + " 原因是：" + rs.result, "提示");
				}
			}
		});
	},
	load : function(url, errorTips) {
		var result;
		$.ajax({
			url : url,
			type : "GET",
			async : false,
			dataType : "json",
			contentType : 'application/json;charset=utf-8',
			success : function(rs) {
				if (rs.success) {
					result = rs.result;
				} else {
					App.myDialog(errorTips + "原因是：" + rs.resultMessage, "提示");
				}
			}
		});

		return result;
	}
};

var App = {
	myDialog : function(content, dialogName, tempBtnName, func) {
		if ($('#tempDialog').length != 0) {
			$('#tempDialog').remove();
		}

		var con = "<div class='modal fade' id='tempDialog' tabindex='-1' role='dialog' "
				+ "aria-labelledby='addDialog' aria-hidden='true'>"
				+ "<div class='modal-dialog'>"
				+ "<div class='modal-content'>"
				+ "<div class='modal-header'>"
				+ "<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>"
				+ "<h4 class='modal-title'>"
				+ dialogName
				+ "</h4>"
				+ "</div>"
				+ "<div class='modal-body'>"
				+ content
				+ "</div>"
				+ "<div class='modal-footer'>"
				+ "<button type='button' class='btn btn-default' data-dismiss='modal'>关闭</button>"
				+ "<button type='button' id='tempBtn' class='btn btn-primary'>"
				+ tempBtnName
				+ "</button>"
				+ "</div>"
				+ "</div><!-- /.modal-content -->"
				+ "</div><!-- /.modal -->"
				+ "</div>";
		// 关闭 弹框
		$('body').append(con);
		// btn 存在 确定 取消按钮显示
		if (tempBtnName == undefined) {
			$('#tempBtn').hide();
		}else {
			$('#tempBtn').on('click', func);
		}
		
		$("#tempDialog").modal('show');
	},
	seSelectConcept : function(element, concepts) {
		$("#" + element).html("");
		$("#" + element).append(
				"<option value='unselect'>未选择</option>");
		for (var i = 0; i < concepts.length; i++) {
			$("#" + element).append(
					"<option value='" + concepts[i].concept_id
							+ "'>" + concepts[i].concept_name
							+ "</option>");
		}
	}
}

// 扩展Date的format方法
Date.prototype.format = function (format) {
	var o = {
		"M+": this.getMonth() + 1,
		"d+": this.getDate(),
		"h+": this.getHours(),
		"m+": this.getMinutes(),
		"s+": this.getSeconds(),
		"q+": Math.floor((this.getMonth() + 3) / 3),
		"S": this.getMilliseconds()
	}
	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}

/**
 * 转换long值为日期字符串
 * 
 * @param l
 *            long值
 * @param pattern
 *            格式字符串,例如：yyyy-MM-dd hh:mm:ss
 * @return 符合要求的日期字符串
 */
function getFormatDateByLong(l, pattern) {
	return getFormatDate(new Date(l), pattern);
}
/**
 * 转换日期对象为日期字符串
 * 
 * @param l
 *            long值
 * @param pattern
 *            格式字符串,例如：yyyy-MM-dd hh:mm:ss
 * @return 符合要求的日期字符串
 */
function getFormatDate(date, pattern) {
	if (date == undefined) {
		date = new Date();
	}
	if (pattern == undefined) {
		pattern = "yyyy-MM-dd hh:mm:ss";
	}
	return date.format(pattern);
}